package db

import (
	"praiselife_gin/library/config"
	"praiselife_gin/library/logger"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

var DB *gorm.DB

func init() {
	user := config.GetString("DATABASE.USER")
	pass := config.GetString("DATABASE.PASS")
	host := config.GetString("DATABASE.HOST")
	port := config.GetString("DATABASE.PORT")
	dbname := config.GetString("DATABASE.DBNAME")
	charset := config.GetString("DATABASE.CHARSET")
	dsn := user + ":" + pass + "@tcp(" + host + ":" + port + ")/" + dbname + "?charset=" + charset + "&parseTime=True&loc=Local"
	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			//TablePrefix:   "fa_", // 表名前缀，`User`表为`t_users`
			SingularTable: true, // 使用单数表名，启用该选项后，`User` 表将是`user`
		},
	})
	if err != nil {
		logger.LogError(err)
	}
}
